home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / SDL / examples / testwin.c < prev   
C/C++ Source or Header  |  2002-10-27  |  9KB  |  370 lines

  1.  
  2. /* Bring up a window and play with it */
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8.  
  9. #define BENCHMARK_SDL
  10.  
  11. #define NOTICE(X)    printf("%s", X);
  12.  
  13. #include "SDL.h"
  14.  
  15. #include <inline/SDL.h>
  16.  
  17. void DrawPict(SDL_Surface *screen, char *bmpfile,
  18.                     int speedy, int flip, int nofade)
  19. {
  20.     SDL_Surface *picture;
  21.     SDL_Rect dest, update;
  22.     int i, centered;
  23.     int ncolors;
  24.     SDL_Color *colors, *cmap;
  25.  
  26.     /* Load the image into a surface */
  27.     if ( bmpfile == NULL ) {
  28.         bmpfile = "sample.bmp";        /* Sample image */
  29.     }
  30. //    printf( "Loading picture: %s\n", bmpfile);
  31.     picture = SDL_LoadBMP(bmpfile);
  32.  
  33.  
  34.     if ( picture == NULL ) {
  35.         fprintf(stderr, "Couldn't load %s: %s\n", bmpfile,
  36.                             SDL_GetError());
  37.         return;
  38.     }
  39.  
  40.     /* Set the display colors -- on a hicolor display this is a no-op */
  41.     if ( picture->format->palette ) {
  42.         ncolors = picture->format->palette->ncolors;
  43.         colors  = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
  44.         cmap    = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
  45.         memcpy(colors, picture->format->palette->colors,
  46.                         ncolors*sizeof(SDL_Color));
  47.     } else {
  48.         int       r, g, b;
  49.  
  50.         /* Allocate 256 color palette */
  51.         ncolors = 256;
  52.         colors  = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
  53.         cmap    = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
  54.  
  55.         /* Set a 3,3,2 color cube */
  56.         for ( r=0; r<8; ++r ) {
  57.             for ( g=0; g<8; ++g ) {
  58.                 for ( b=0; b<4; ++b ) {
  59.                     i = ((r<<5)|(g<<2)|b);
  60.                     colors[i].r = r<<5;
  61.                     colors[i].g = g<<5;
  62.                     colors[i].b = b<<6;
  63.                 }
  64.             }
  65.         }
  66.     }
  67. NOTICE("testwin: setting colors\n");
  68.     if ( ! SDL_SetColors(screen, colors, 0, ncolors) &&
  69.                 (screen->format->palette != NULL) ) {
  70.         fprintf(stderr,
  71. "Warning: Couldn't set all of the colors, but SDL will map the image\n"
  72. "         (colormap fading will suffer - try the -warp option)\n"
  73.         );
  74.     }
  75.  
  76.     /* Set the screen to black (not really necessary) */
  77.     if ( SDL_LockSurface(screen) == 0 ) {
  78.         Uint32 black;
  79.         Uint8 *pixels;
  80.  
  81.         black = SDL_MapRGB(screen->format, 0, 0, 0);
  82.         pixels = (Uint8 *)screen->pixels;
  83.         for ( i=0; i<screen->h; ++i ) {
  84.             memset(pixels, black,
  85.                 screen->w*screen->format->BytesPerPixel);
  86.             pixels += screen->pitch;
  87.         }
  88.         SDL_UnlockSurface(screen);
  89.         SDL_UpdateRect(screen, 0, 0, 0, 0);
  90.     }
  91.     
  92.     /* Display the picture */
  93.     if ( speedy ) {
  94.         SDL_Surface *displayfmt;
  95.  
  96. fprintf(stderr, "Converting picture\n");
  97.         displayfmt = SDL_DisplayFormat(picture);
  98.         if ( displayfmt == NULL ) {
  99.             fprintf(stderr,
  100.                 "Couldn't convert image: %s\n", SDL_GetError());
  101.             goto done;
  102.         }
  103.         SDL_FreeSurface(picture);
  104.         picture = displayfmt;
  105.     }
  106.     printf("(image surface located in %s memory)\n", 
  107.             (picture->flags&SDL_HWSURFACE) ? "video" : "system");
  108.     centered = (screen->w - picture->w)/2;
  109.     if ( centered < 0 ) {
  110.         centered = 0;
  111.     }
  112.     dest.y = (screen->h - picture->h)/2;
  113.     dest.w = picture->w;
  114.     dest.h = picture->h;
  115. NOTICE("testwin: moving image\n");
  116.     for ( i=0; i<=centered; ++i ) {
  117.         dest.x = i;
  118.         update = dest;
  119.         if ( SDL_BlitSurface(picture, NULL, screen, &update) < 0 ) {
  120.             fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
  121.             break;
  122.         }
  123.         if ( flip ) {
  124.             SDL_Flip(screen);
  125.         } else {
  126.             SDL_UpdateRects(screen, 1, &update);
  127.         }
  128.     }
  129.  
  130. #ifdef SCREENSHOT
  131.     if ( SDL_SaveBMP(screen, "screen.bmp") < 0 )
  132.         printf("Couldn't save screen: %s\n", SDL_GetError());
  133. #endif
  134.  
  135. #ifndef BENCHMARK_SDL
  136.     /* Let it sit there for a while */
  137.     SDL_Delay(5*1000);
  138. #endif
  139.     /* Fade the colormap */
  140.     if ( ! nofade ) {
  141.         int maxstep;
  142.         SDL_Color final;
  143.         SDL_Color palcolors[256];
  144.         struct {
  145.             Sint16 r, g, b;
  146.         } cdist[256];
  147.  
  148. NOTICE("testwin: fading out...\n");
  149.         memcpy(cmap, colors, ncolors*sizeof(SDL_Color));
  150.         maxstep = 32-1;
  151.         final.r = 0xFF;
  152.         final.g = 0x00;
  153.         final.b = 0x00;
  154.         memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
  155.         for ( i=0; i<ncolors; ++i ) {
  156.             cdist[i].r = final.r-palcolors[i].r;
  157.             cdist[i].g = final.g-palcolors[i].g;
  158.             cdist[i].b = final.b-palcolors[i].b;
  159.         }
  160.         for ( i=0; i<=maxstep/2; ++i ) {    /* halfway fade */
  161.             int c;
  162.             for ( c=0; c<ncolors; ++c ) {
  163.                 colors[c].r =
  164.                     palcolors[c].r+((cdist[c].r*i))/maxstep;
  165.                 colors[c].g =
  166.                     palcolors[c].g+((cdist[c].g*i))/maxstep;
  167.                 colors[c].b =
  168.                     palcolors[c].b+((cdist[c].b*i))/maxstep;
  169.             }
  170.             SDL_SetColors(screen, colors, 0, ncolors);
  171.             SDL_Delay(1);
  172.         }
  173.         final.r = 0x00;
  174.         final.g = 0x00;
  175.         final.b = 0x00;
  176.         memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
  177.         for ( i=0; i<ncolors; ++i ) {
  178.             cdist[i].r = final.r-palcolors[i].r;
  179.             cdist[i].g = final.g-palcolors[i].g;
  180.             cdist[i].b = final.b-palcolors[i].b;
  181.         }
  182.         maxstep /= 2;
  183.         for ( i=0; i<=maxstep; ++i ) {        /* finish fade out */
  184.             int c;
  185.             for ( c=0; c<ncolors; ++c ) {
  186.                 colors[c].r =
  187.                     palcolors[c].r+((cdist[c].r*i))/maxstep;
  188.                 colors[c].g =
  189.                     palcolors[c].g+((cdist[c].g*i))/maxstep;
  190.                 colors[c].b =
  191.                     palcolors[c].b+((cdist[c].b*i))/maxstep;
  192.             }
  193.             SDL_SetColors(screen, colors, 0, ncolors);
  194.             SDL_Delay(1);
  195.         }
  196.         for ( i=0; i<ncolors; ++i ) {
  197.             colors[i].r = final.r;
  198.             colors[i].g = final.g;
  199.             colors[i].b = final.b;
  200.         }
  201.         SDL_SetColors(screen, colors, 0, ncolors);
  202. NOTICE("testwin: fading in...\n");
  203.         memcpy(palcolors, colors, ncolors*sizeof(SDL_Color));
  204.         for ( i=0; i<ncolors; ++i ) {
  205.             cdist[i].r = cmap[i].r-palcolors[i].r;
  206.             cdist[i].g = cmap[i].g-palcolors[i].g;
  207.             cdist[i].b = cmap[i].b-palcolors[i].b;
  208.         }
  209.         for ( i=0; i<=maxstep; ++i ) {    /* 32 step fade in */
  210.             int c;
  211.             for ( c=0; c<ncolors; ++c ) {
  212.                 colors[c].r =
  213.                     palcolors[c].r+((cdist[c].r*i))/maxstep;
  214.                 colors[c].g =
  215.                     palcolors[c].g+((cdist[c].g*i))/maxstep;
  216.                 colors[c].b =
  217.                     palcolors[c].b+((cdist[c].b*i))/maxstep;
  218.             }
  219.             SDL_SetColors(screen, colors, 0, ncolors);
  220.             SDL_Delay(1);
  221.         }
  222. NOTICE("testwin: fading over\n");
  223.     }
  224.     
  225. done:
  226.     /* Free the picture and return */
  227.     SDL_FreeSurface(picture);
  228.     free(colors); free(cmap);
  229.     return;
  230. }
  231.  
  232. int main(int argc, char *argv[])
  233. {
  234.     SDL_Surface *screen;
  235.     /* Options */
  236.     int speedy, flip, nofade;
  237.     int delay;
  238.     int w, h;
  239.     int desired_bpp;
  240.     Uint32 video_flags;
  241. #ifdef BENCHMARK_SDL
  242.     Uint32 then, now;
  243. #endif
  244.     /* Set default options and check command-line */
  245.     speedy = 0;
  246.     flip = 0;
  247.     nofade = 0;
  248.     delay = 1;
  249.     w = 640;
  250.     h = 480;
  251.     desired_bpp = 0;
  252.     video_flags = 0;
  253.     while ( argc > 1 ) {
  254.         if ( strcmp(argv[1], "-speedy") == 0 ) {
  255.             speedy = 1;
  256.             argv += 1;
  257.             argc -= 1;
  258.         } else
  259.         if ( strcmp(argv[1], "-nofade") == 0 ) {
  260.             nofade = 1;
  261.             argv += 1;
  262.             argc -= 1;
  263.         } else
  264.         if ( strcmp(argv[1], "-delay") == 0 ) {
  265.             if ( argv[2] ) {
  266.                 delay = atoi(argv[2]);
  267.                 argv += 2;
  268.                 argc -= 2;
  269.             } else {
  270.                 fprintf(stderr,
  271.                 "The -delay option requires an argument\n");
  272.                 exit(1);
  273.             }
  274.         } else
  275.         if ( strcmp(argv[1], "-width") == 0 ) {
  276.             if ( argv[2] && ((w = atoi(argv[2])) > 0) ) {
  277.                 argv += 2;
  278.                 argc -= 2;
  279.             } else {
  280.                 fprintf(stderr,
  281.                 "The -width option requires an argument\n");
  282.                 exit(1);
  283.             }
  284.         } else
  285.         if ( strcmp(argv[1], "-height") == 0 ) {
  286.             if ( argv[2] && ((h = atoi(argv[2])) > 0) ) {
  287.                 argv += 2;
  288.                 argc -= 2;
  289.             } else {
  290.                 fprintf(stderr,
  291.                 "The -height option requires an argument\n");
  292.                 exit(1);
  293.             }
  294.         } else
  295.         if ( strcmp(argv[1], "-bpp") == 0 ) {
  296.             if ( argv[2] ) {
  297.                 desired_bpp = atoi(argv[2]);
  298.                 argv += 2;
  299.                 argc -= 2;
  300.             } else {
  301.                 fprintf(stderr,
  302.                 "The -bpp option requires an argument\n");
  303.                 exit(1);
  304.             }
  305.         } else
  306.         if ( strcmp(argv[1], "-warp") == 0 ) {
  307.             video_flags |= SDL_HWPALETTE;
  308.             argv += 1;
  309.             argc -= 1;
  310.         } else
  311.         if ( strcmp(argv[1], "-hw") == 0 ) {
  312.             video_flags |= SDL_HWSURFACE;
  313.             argv += 1;
  314.             argc -= 1;
  315.         } else
  316.         if ( strcmp(argv[1], "-flip") == 0 ) {
  317.             video_flags |= SDL_DOUBLEBUF;
  318.             argv += 1;
  319.             argc -= 1;
  320.         } else
  321.         if ( strcmp(argv[1], "-fullscreen") == 0 ) {
  322.             video_flags |= SDL_FULLSCREEN;
  323.             argv += 1;
  324.             argc -= 1;
  325.         } else
  326.             break;
  327.     }
  328.  
  329.     if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
  330.         fprintf(stderr,
  331.             "Couldn't initialize SDL: %s\n", SDL_GetError());
  332.         exit(1);
  333.     }
  334.     atexit(SDL_Quit);            /* Clean up on exit */
  335.  
  336.     /* Initialize the display */
  337.     screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
  338.     if ( screen == NULL ) {
  339.         fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
  340.                     w, h, desired_bpp, SDL_GetError());
  341.         exit(1);
  342.     }
  343.     printf("Set%s %dx%dx%d mode\n",
  344.             screen->flags & SDL_FULLSCREEN ? " fullscreen" : "",
  345.             screen->w, screen->h, screen->format->BitsPerPixel);
  346.     printf("(video surface located in %s memory)\n",
  347.             (screen->flags&SDL_HWSURFACE) ? "video" : "system");
  348.     if ( screen->flags & SDL_DOUBLEBUF ) {
  349.         printf("Double-buffering enabled\n");
  350.         flip = 1;
  351.     }
  352.  
  353.     /* Set the window manager title bar */
  354.     SDL_WM_SetCaption("SDL test window", "testwin");
  355.  
  356.     /* Do all the drawing work */
  357. //    printf("qui ci arrivo?\n");
  358.  
  359. #ifdef BENCHMARK_SDL
  360.     then = SDL_GetTicks();
  361.     DrawPict(screen, argv[1], speedy, flip, nofade);
  362.     now = SDL_GetTicks();
  363.     printf("Time: %d milliseconds\n", now-then);
  364. #else
  365.     DrawPict(screen, argv[1], speedy, flip, nofade);
  366. #endif
  367.     SDL_Delay(delay*1000);
  368.     return(0);
  369. }
  370.